package com.pavlinic.mylisp;

import java.util.LinkedList;
import java.util.List;

import com.pavlinic.mylisp.specialForms.SpecialForm;

public class ListExpression implements Expression {

	private final List<Expression> list;
	
	//really great for debugging but needs to be taken out when the system is
	//more stable and replaced with a good tracing mechanism
	public static boolean TRACE = true;

	public ListExpression() {
		this(new LinkedList<Expression>());
	}

	private ListExpression(List<Expression> list) {
		this.list = list;
	}

	public void add(Expression expression) {
		list.add(expression);
	}

	// TODO: This is horrific!!! Refactor!
	public Expression evaluate(EvaluationContext context) {
		if (TRACE ) {
			System.out.println(this);
		}
		if (isEmpty()) {
			return this;
		}
		final Expression first = getFunction(context);
		if (first instanceof Function) {
			final Expression result = ((Function) first).call(rest(), context);
			return result;
		}

		if (first instanceof ValueExpression) {
			return first;
		}

		final String symbol = ((SymbolExpression) first).getValue();
		if (SpecialForm.isSpecialForm(symbol)) {
			return SpecialForm.evaluate(symbol, this, context);
		} else {
			final Function function = (Function) context.getBinding(symbol);
			if (function == null) {
				throw new NoSuchBindingException(symbol);
			}
			return function.call(rest(), context);
		}
	}

	private Expression getFunction(EvaluationContext context) {
		final Expression first = first();
		if (first instanceof ListExpression) {
			return first.evaluate(context);
		}
		return first;
	}

	public ListExpression rest() {
		final List<Expression> rest = list.subList(1, size());
		if (rest.isEmpty()) {
			return NullExpression.INSTANCE;
		}
		return new ListExpression(rest);
	}

	public Expression first() {
		return list.get(0);
	}

	@Override
	public String toString() {
		final StringBuilder buf = new StringBuilder();
		buf.append("(");
		boolean first = true;
		for (Expression expr : list) {
			if (!first) {
				buf.append(" ");
			} else {
				first = false;
			}
			buf.append(expr.toString());
		}
		buf.append(")");
		return buf.toString();
	}

	public boolean isEmpty() {
		return list.isEmpty();
	}

	public Expression get(int i) {
		return list.get(i);
	}

	public int size() {
		return list.size();
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((list == null) ? 0 : list.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		ListExpression other = (ListExpression) obj;
		if (list == null) {
			if (other.list != null)
				return false;
		} else if (!list.equals(other.list))
			return false;
		return true;
	}

	public Object getValue() {
		// Makes sense???
		return list;
	}

	public void addAll(ListExpression tail) {
		for (int i = 0; i < tail.size(); i++) {
			add(tail.get(i));
		}
	}

}
